package com.xianzhiOA.service.impl;/**
 * Created by Administrator on 2017/5/26.
 */

import com.github.pagehelper.PageHelper;
import com.xianzhiOA.bean.XzWeek;
import com.xianzhiOA.bean.XzWork;
import com.xianzhiOA.dao.XzWeekMapper;
import com.xianzhiOA.dao.XzWorkMapper;
import com.xianzhiOA.service.XzWeekService;
import com.xianzhiOA.service.XzWorkService;
import com.xianzhiOA.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 工作量概况业务层
 *
 * @author 甘汝雷
 * @create 2017-05-26 14:33
 **/
@Service("XzWeekService")
public class XzWeekServiceImpl implements XzWeekService {
    @Autowired
    private XzWeekMapper weekMapper;
    @Autowired
    private XzWorkService workService;
    @Autowired
    private XzWorkMapper workMapper;
    @Override
    public int insert(XzWeek week) {
        //添加工作量信息
        weekMapper.insert(week);
        List<Date> dates = dateToWeek(week.getWeekTime());
        double a=0,b=0;
        for (int i =0;i<week.getWorkList().size();i++) {
            week.getWorkList().get(i).setWorkWeekTime(dates.get(i));//添加日期
            week.getWorkList().get(i).setWorkWeek(workService.getWeekOfDate(dates.get(i)));//添加星期
            week.getWorkList().get(i).setWeekId(week.getWeekId());//添加weekId
            week.getWorkList().get(i).setEmpId(week.getEmpId());//添加员工id
            if (week.getWorkList().get(i).getWorkActual()==0)
                week.getWorkList().get(i).setWorkActual(workService.workActual(week.getWorkList().get(i),dates.get(i)));//添加实际工作时间
            workMapper.insert(week.getWorkList().get(i));
            a += week.getWorkList().get(i).getWorkPlan();//统计周计划时间
            b += week.getWorkList().get(i).getWorkActual();//统计周实际时间
        }
        week.setWeekPlan(a);//保存周计划时间
        week.setWeekActual(b);//保存周实际时间
        weekMapper.update(myMap(week));//把计划和实际时间添加到周工作量表中
        return 0;
    }
    //根据当前日期获得这一周的日期
    public static List<Date> dateToWeek(Date mdate) {
        Calendar calendar = Calendar.getInstance();
        if(mdate != null){
            calendar.setTime(mdate);
        }
        int b = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        Date fdate ;
        List <Date> list = new ArrayList();
        Long fTime=mdate.getTime()-(b-1)*24*3600000;
        for(int a=0;a<8;a++)
        {
            fdate= new Date();
            fdate.setTime(fTime+(a*24*3600000));
            list.add(a, fdate);
        }

        return list;
    }
    @Override
    //把字符串类型转换成Date
    public Date myDate(String string){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//小写的mm表示的是分钟
        Date date = null;
        try {
            date=sdf.parse(string);
        }catch (Exception e){

        }
        return date;
    }
    //删除工作量信息
    @Override
    public int delete(Long weekId) {
        return weekMapper.delete(weekId);
    }
    //伪删除工作量信息
    @Override
    public int isdelete(Long weekId) {
        Map map = new HashMap();
        map.put("weekId",weekId);
        map.put("isdelete",1);
        return weekMapper.update(map);
    }
    //修改工作量时间
    @Override
    public int update(XzWeek week) {
        List<Date> dates = dateToWeek(week.getWeekTime());//
        double a=0,b=0;
        for (int i =0;i<week.getWorkList().size();i++) {
            week.getWorkList().get(i).setWorkWeekTime(dates.get(i));//添加日期
            week.getWorkList().get(i).setWorkWeek(workService.getWeekOfDate(dates.get(i)));//添加星期
            week.getWorkList().get(i).setWeekId(week.getWeekId());//添加weekId
            week.getWorkList().get(i).setEmpId(week.getEmpId());//添加员工id
            if (week.getWorkList().get(i).getWorkActual()==0)
                week.getWorkList().get(i).setWorkActual(workService.workActual(week.getWorkList().get(i),dates.get(i)));//添加实际工作时间
            workMapper.update(workMap(week.getWorkList().get(i)));
            a += week.getWorkList().get(i).getWorkPlan();//统计周计划时间
            b += week.getWorkList().get(i).getWorkActual();//统计周实际时间
        }
        week.setWeekPlan(a);//保存周计划时间
        week.setWeekActual(b);//保存周实际时间
        return weekMapper.update(myMap(week));
    }
    //把work转换成work类型
    public Map workMap(XzWork work){
        Map map = new HashMap();
        map.put("workId",work.getWorkId());
        map.put("workWeek",work.getWorkWeek());
        map.put("workPlan",work.getWorkPlan());
        map.put("workActual",work.getWorkActual());
        map.put("weekId",work.getWeekId());
        map.put("workWeekId",work.getWorkWeekTime());
        map.put("empId",work.getEmpId());
        return map;
    }
    //把week转换成Map类型
    public Map myMap(XzWeek week){
        Map map = new HashMap();
        map.put("weekId",week.getWeekId());
        map.put("weekPlan",week.getWeekPlan());
        map.put("weekActual",week.getWeekActual());
        map.put("weekProject",week.getWeekProject());
        map.put("weekTime",week.getWeekTime());
        map.put("empId",week.getEmpId());
        map.put("empName",week.getEmpName());
        map.put("projectName",week.getProjectName());
        return map;
    }
    //分页查询工作量信息
    @Override
    public PageBean<XzWeek> selAll(XzWeek week, int page, int rows, Date begin, Date end, Date beginDate, Date endDate) {
        PageHelper.startPage(page,rows);
        Map map = myMap(week);
        if (week.getWeekString()!=""&&week.getWeekString()!=null){
            Date date = myDate(week.getWeekString());
            Map byDate = workService.getWeekByDate(date);
            map.putAll(byDate);
        }
        List<XzWeek> weekList = weekMapper.selAll(map);
        for (XzWeek w:weekList) {
            Map byDate = workService.getWeekByDate(w.getWeekTime());
            w.setWeekTimeBegin((String)byDate.get("begin"));//获取当周的开始时间
            w.setWeekTimeEnd((String)byDate.get("end"));//获取当周的结束时间
            w.setDeviation(myDouble(w.getWeekActual()-w.getWeekPlan()));//保存偏差值
            if (w.getDeviation()<0)w.setRate(myDouble(Math.abs(w.getDeviation()*100/w.getWeekPlan()))*-1);//保存偏差率(偏差率伪负数)
            if (w.getDeviation()>0)w.setRate(myDouble(w.getDeviation()*100/w.getWeekPlan()));//保存偏差率
        }
        PageBean pageBean = new PageBean(weekList);
        return pageBean;
    }
    //查询所有工作量信息
    @Override
    public List<XzWeek> selAll(Map map) {
        return weekMapper.selAll(map);
    }
    //double类型数据保留一位小数
    public double myDouble(double d){
        BigDecimal bd = new BigDecimal(d);
        return bd.setScale(1,BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    //通过id进行查询
    @Override
    public XzWeek selById(Long weekId) {
        XzWeek week = weekMapper.selById(weekId);
        Map byDate = workService.getWeekByDate(week.getWeekTime());
        week.setWeekTimeBegin((String)byDate.get("begin"));//获取当周的开始时间
        week.setWeekTimeEnd((String)byDate.get("end"));//获取当周的结束时间
        week.setDeviation(myDouble(week.getWeekActual()-week.getWeekPlan()));//保存偏差值
        week.setRate(myDouble(week.getDeviation()-week.getWeekPlan()));//保存偏差率
        for (XzWork w: week.getWorkList()) {
            w.setDeviation(myDouble(w.getWorkActual()-w.getWorkPlan()));//保存偏差值
            w.setRate(myDouble(w.getDeviation()-w.getWorkPlan()));//保存偏差率
        }
        return week;
    }
}
